<?php
namespace PsiFramework\Applications\AlloMediaUser\Classes\Actions;

import("Applications.AlloMediaPortal.Classes.Actions.AlloMediaUserAction");
import("Classes.Util.HTTP.AuthenticationManager");
import("Classes.Factory.DAOFactory");
import("Plugins.Shop.Classes.Model.Order");

use \PsiFramework\Applications\AlloMediaPortal\Classes\Actions\AlloMediaUserAction;
use \PsiFramework\Classes\Util\HTTP\AuthenticationManager;
use \PsiFramework\Classes\Util\Parameters\Action\ActionParameters;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Plugins\Shop\Classes\Model\Order;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de l'index
 *
 * @package Actions.Pages
 */
class DeleteBasketItemAction extends AlloMediaUserAction
{

    public function __construct(ActionParameters $actionParameters)
    {
        parent::__construct($actionParameters);
    }

    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     *
     */
    public function execute()
    {
        parent::execute();
        
        $orderDao = DAOFactory::getDAO('Order');
        $order = $orderDao->retrieveActiveOrderByUser($this->_user, false);
        
        $orderItemId = $this->_request->getParameter('order_item_id');
        
        $linkDao = DAOFactory::getDAO('OrderItemLink');
        $links = $order->getItems();
        foreach ($links as $index=>$link) {
            if ($orderItemId == $link->getItem()->getId()) {
            	$linkDao->delete($link);
            	unset($links[$index]);
            	break;
            }
        }
        $order->setItems($links);
        
        $this->findForward('success');
    }
}
